package com.rays.sysmech.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagLayout;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

import javax.rmi.CORBA.Util;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


import com.rays.sysmech.constants.StringConstants;
import com.rays.sysmech.core.DuplicateDataInfo;
import com.rays.sysmech.core.FileData;
import com.rays.sysmech.listeners.BrowseBtnListner;
import com.rays.sysmech.listeners.StartBtnListner;
import com.rays.sysmech.utils.Utils;

public class MainWindow extends JFrame 
{
	File selectedFile;
	private static MainWindow mainWnd;
	JLabel selectedLocLbl;
	JLabel sizeLbl;
	JPanel jPanel;
	JMenuItem startItem;
	JMenuBar mBar;
	GridBagLayout grdLayout = new GridBagLayout();
	
	private MainWindow()
	{
		this.initializeWindow();
		this.validate();
		this.pack();
	}
	
	private void initializeWindow()
	{
		this.setMainWindowPrperties();
		jPanel = new JPanel();
		//jPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
		mBar = new JMenuBar();
		this.setJMenuBar(mBar);
		JMenu filemenu = new JMenu(StringConstants.fileMenu);
		
		mBar.add(filemenu);
		
		this.getContentPane().add(jPanel, BorderLayout.WEST);

		JMenuItem browseItem = new JMenuItem(StringConstants.browseBtn);
		browseItem.setToolTipText(StringConstants.browseTip);
		browseItem.addActionListener(new BrowseBtnListner());
		filemenu.add(browseItem);
		
		startItem = new JMenuItem(StringConstants.startBtn);
		startItem.setToolTipText(StringConstants.startTip);
		startItem.setEnabled(false);
		startItem.addActionListener(new StartBtnListner());
		filemenu.add(startItem);
		
		
		sizeLbl = new JLabel();
		sizeLbl.setVisible(true);
		//jPanel.add(sizeLbl, BorderLayout.LINE_END);
		
		selectedLocLbl = new JLabel();
		selectedLocLbl.setVisible(true);
		//jPanel.add(selectedLocLbl, BorderLayout.PAGE_START);
		
		this.addAboutButton();
	}
	
	
	private void addAboutButton() {

		JMenu abtMenu = new JMenu(StringConstants.about);
		JMenuItem abtItem = new JMenuItem(StringConstants.about);
		abtMenu.add(abtItem);
		//abtItem.setIcon(Utils.createImageIcon(StringConstants.frameIconImage));
		abtItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(jPanel, StringConstants.aboutInfo);
				
			}
		});
		mBar.add(abtMenu);
	}

	private void setMainWindowPrperties()
	{
		setTitle(StringConstants.mainWindowTitle);
		setResizable(true);
		setPreferredSize(new Dimension(1400, 800));
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setIconImage(Toolkit.getDefaultToolkit().
				createImage(StringConstants.frameIconImage));
		setBackground(Color.ORANGE);
	}
	
	public static MainWindow getMainWindow()
	{
		if(null == mainWnd)
		{
			mainWnd = new MainWindow();
		}
		return mainWnd;
		
	}
	
	public void addText(String str) {
		/*txtArea.append(str);
		txtArea.setVisible(true);*/
	}
	
	public void setSizeLabel(long size)
	{
		String unit = StringConstants.bytes;
		double totoalSize = size / 1.0;
		if(totoalSize > 1024)
		{
			unit = StringConstants.kiloBytes;
			totoalSize /= 1024;
		}
		if(totoalSize > 1024)
		{
			unit = StringConstants.megaBytes;
			totoalSize /= 1024;
		}
		if(totoalSize > 1024)
		{
			unit = StringConstants.gigaBytes;
			totoalSize /= 1024;
		}
		DecimalFormat df = new DecimalFormat(StringConstants.decimalFormat);
		
		sizeLbl.setText(StringConstants.totalDuplicate + df.format(totoalSize) + " "+ unit);
		sizeLbl.setVisible(true);
	}
	
	public void reset()
	{
		startItem.setEnabled(false);
		//txtArea.setText("");
		//sizeLbl.setVisible(false);
	}
	
	public File getSelectedFile()
	{
		return selectedFile;
	}
	
	public void setSelectedFile(File iFile)
	{
		this.selectedFile = iFile;
		selectedLocLbl.setText("Click Start to find Duplicates in " + iFile.getAbsolutePath());
        startItem.setEnabled(true);
        /*OptionsDialog dialog = new OptionsDialog(this, true);
        dialog.setVisible(true);*/

	}
	
	public void useDuplicatesData(DuplicateDataInfo duplicateData) {
		
		Iterator<Entry<FileData, ArrayList<String>>> itr = duplicateData.getRecordIterator();
		Dimension dimension = new Dimension(150,25);
		JScrollPane content = new JScrollPane();
		content.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		content.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		content.setViewportView(jPanel);
		jPanel.setLayout(grdLayout);

		GridBagConstraints constrnt = new GridBagConstraints();
		constrnt.fill = GridBagConstraints.HORIZONTAL;
		constrnt.gridheight = 5;
		int row = 0;
		while(itr.hasNext())
		{
			Entry<FileData, ArrayList<String>> entry = itr.next();
			FileData filedata = entry.getKey();
			ArrayList<String> locations = entry.getValue();
			
			JButton fileName = new MButton(filedata);
			fileName.setIcon(Utils.createImageIcon(StringConstants.fileViewIcon));
			fileName.setPreferredSize(dimension);
			constrnt.gridx = 0;
			constrnt.gridy = row;
			jPanel.add(fileName, constrnt);
			
			final Object[] arr = locations.toArray();
			JComboBox combox = new JComboBox(arr);
			combox.setPreferredSize(dimension);
			constrnt.gridx = 1;
			constrnt.gridy = row;
			jPanel.add(combox, constrnt);

			JButton deleteSelected = new JButton(StringConstants.deleteBtnLbl);
			deleteSelected.setIcon(Utils.createImageIcon(StringConstants.tempIcon));
			deleteSelected.setPreferredSize(dimension);
			constrnt.gridx = 2;
			constrnt.gridy = row;
			jPanel.add(deleteSelected, constrnt);
			
			JButton keepSelected = new JButton(StringConstants.keepBtnLbl);
			keepSelected.setPreferredSize(dimension);
			keepSelected.setIcon(Utils.createImageIcon(StringConstants.tempIcon));
			constrnt.gridx = 3;
			constrnt.gridy = row;
			jPanel.add(keepSelected, constrnt);
			row++;
		} 
		
		this.setContentPane(content);
		this.pack();
		
	}

}
